package com.sduept.nwld.dataserver.controller.daily;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import org.primefaces.event.CellEditEvent;
import org.primefaces.event.SelectEvent;
import com.sduept.nwld.dataserver.manager.protection.OperationRiskExceedManager;
import com.sduept.nwld.dataserver.model.daily.OperationaRiskExceed;
import com.sduept.nwld.dataserver.model.daily.OperationaRiskExceedDTO;

@Named
@ViewScoped
public class OperationRiskExceedController extends DailyReportAbstractController {

	private static final long serialVersionUID = -1754863802978166665L;
	@Autowired
	private OperationRiskExceedManager manager;

	private List<OperationaRiskExceed> exceedList = new ArrayList<>();
	private List<OperationaRiskExceedDTO> exceedDtos = new ArrayList<>();
	private OperationaRiskExceed select = null;

	@PostConstruct
	public void init() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String start = format.format(queryDate1);
		String end = format.format(queryDate2);
		exceedList = manager.getSumExceedList(start, end);
	}

	public void refreshPage() {
		initParamter();
		init();
	}

	public void resetQuery() {
		queryDate1 = null;
		queryDate2 = null;
		resetqueryDate();
		queryByTime();
	}

	public void queryByTime() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String start = null;
		String end = null;
		if (queryDate1 == null && queryDate2 == null) {
			exceedList = manager.findAll();
		} else {
			start = "1970-01-01 00:00:00";
			end = format.format(Calendar.getInstance().getTime());
			if (queryDate1 != null) {
				start = format.format(queryDate1);
			}
			if (queryDate2 != null) {
				end = format.format(queryDate2);
			}
		}
		exceedList = manager.getSumExceedList(start, end);
		refreshQueryParamter();
	}

	public void cellEdit(CellEditEvent event) {
		select = null;
		for (OperationaRiskExceed k : exceedList) {
			if (k.getId().equals(event.getRowKey())) {
				select = k;
				break;
			}
		}
		if (select != null) {
			manager.updateRiskExceed(select);
			select.setSum(select.getDirectTransferNum() + select.getGuangdongNum() + select.getGuangxiNum()
					+ select.getGuizhouNum() + select.getHainanNum() + select.getYunnanNum());
			addSuccessMessage("修改成功!");
		}
		select = null;
	}

	public void onRowSelect(SelectEvent event) {
		select = (OperationaRiskExceed) event.getObject();
	}

	public OperationaRiskExceed getSelect() {
		return select;
	}

	public void setSelect(OperationaRiskExceed select) {
		this.select = select;
	}

	public List<OperationaRiskExceedDTO> getExceedDtos() {
		return exceedDtos;
	}

	public void setExceedDtos(List<OperationaRiskExceedDTO> exceedDtos) {
		this.exceedDtos = exceedDtos;
	}

	public List<OperationaRiskExceed> getExceedList() {
		return exceedList;
	}

	public void setExceedList(List<OperationaRiskExceed> exceedList) {
		this.exceedList = exceedList;
	}

}
